<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
load()->func('tpl');
$shset=pdo_get('zh_gjw_shset',array('uniacid'=>$_W['uniacid']));
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
if (!empty($_GPC['state'])) {
    $where=' WHERE  uniacid=:uniacid and state=:state ';
    $data[':uniacid']=$_W['uniacid'];
    $data[':state']=$_GPC['state'];
    if(isset($_GPC['keywords'])){
        $where.="  and (title LIKE  concat('%', :name,'%') or phone LIKE  concat('%', :name,'%'))";
        $data[':name']=$_GPC['keywords']; 
    }
    $active=$_GPC['state'];
    $sql="SELECT * FROM ".tablename('zh_gjw_shotpublish') .$where." ORDER BY time desc";
    $total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zh_gjw_shotpublish').$where." ORDER BY time desc",$data);
}else{
    $where=' WHERE  uniacid=:uniacid  ';
    $data[':uniacid']=$_W['uniacid'];
    if(isset($_GPC['keywords'])){
        $where.="  and (title LIKE  concat('%', :name,'%') or phone LIKE  concat('%', :name,'%'))";
        $data[':name']=$_GPC['keywords']; 
    }
    $active=$_GPC['state'];
    $sql="SELECT * FROM ".tablename('zh_gjw_shotpublish') .$where." ORDER BY time desc";
    $total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zh_gjw_shotpublish').$where." ORDER BY time desc",$data);
}
$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$registers=pdo_fetchall($select_sql,$data);
$pager = pagination($total, $pageindex, $pagesize);

if($op=='delete'){
    $file=pdo_get('zh_gjw_shotpublish',array('id'=>$_GPC['id']));
    $imgs=explode(",",$file['img']);
    $url=str_replace('https://','',$_W['attachurl_local']);
    unlink('/www/wwwroot/'.$url.$file['video']);
    for ($i=0; $i <count($imgs) ; $i++) { 
      unlink('/www/wwwroot/'.$url.$imgs[$i]);
    }
    $res=pdo_delete('zh_gjw_shotpublish',array('id'=>$_GPC['id']));
    // var_dump($res);die;
    if($res){
      message('删除成功',$this->createWebUrl('shotmanage',array()),'success');
    }else{
      message('删除失败','','error');
    }
}
if($_GPC['op']=='tg'){
  include_once IA_ROOT . '/addons/zh_gjw/cert/WxPay.Api.php';
  load()->model('account');
  load()->func('communication');
  $WxPayApi = new WxPayApi();
  $input = new WxPayRefund();
  $path_cert = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_cert_' . $_W['uniacid'] . '.pem';
  $path_key = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_key_' . $_W['uniacid'] . '.pem';
  $refund_order =pdo_get('zh_gjw_shotpublish',array('id'=>$_GPC['id']));  
  // if ($refund_order['pay']==2) {
      
        /*function getaccess_token($_W){
          $res=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
          $appid=$res['appid'];
          $secret=$res['appsecret'];
          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
          $data = curl_exec($ch);
          curl_close($ch);
          $data = json_decode($data,true);
          return $data['access_token'];
        }*/
        function set_msg($_W,$_GPC){
            //$access_token = getaccess_token($_W);
            $ress=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
            $user=pdo_get('zh_gjw_shotpublish',array('id'=>$_GPC['id']));
            $openid=pdo_get('zh_gjw_register',array('phone'=>$user['phone']));
            $formwork ='{
                "touser": "'.$openid["openid"].'",
                "template_id": "'.$ress["templeid"].'",
                "form_id":"'.$user['formid'].'",
                "data": {
                    "keyword1": {
                        "value": "'.$user['title'].'",
                        "color": "#173177"
                    },
                    "keyword2": {
                        "value":"微拍审核通过",
                        "color": "#173177"
                    }
                }  
            }';
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$ress['accessToken']."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
            $data = curl_exec($ch);
            curl_close($ch);
           return $data;
        }
        echo set_msg($_W,$_GPC);
        $res=pdo_update('zh_gjw_shotpublish',array('state'=>2,'shtime'=>date('Y-m-d H:i:s',time())),array('id'=>$_GPC['id']));
        if($res){
          message('', 'referer', 'success');
        }else{
              message('失败！','','error');
        }
  // }
}
if($_GPC['op']=='jj'){
  include_once IA_ROOT . '/addons/zh_gjw/cert/WxPay.Api.php';
  load()->model('account');
  load()->func('communication');
  $WxPayApi = new WxPayApi();
  $input = new WxPayRefund();
  $path_cert = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_cert_' . $_W['uniacid'] . '.pem';
  $path_key = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_key_' . $_W['uniacid'] . '.pem';
  $refund_order =pdo_get('zh_gjw_shotpublish',array('id'=>$_GPC['id']));
        if (empty($refund_order['paycode'])) {
          message('发布时，支付未设置，所以此条未支付',$this->createWebUrl('shotmanage',array()),'success');
        }
        $res_fee=$refund_order['payfee'];
        $shotmoney =pdo_get('zh_gjw_shotmoney',array('uniacid'=>$_W['uniacid'])); 
        if ($refund_order['pay']==1) {
          if ($refund_order['paycode']) { 
            $res=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
            $appid=$res['appid'];
            $key=$res['apikey'];
            $mchid=$res['mac_id'];
            $out_trade_no=$refund_order['paycode'];//商户订单号
            $fee = $res_fee*100;
             //var_dump($fee);die;
            $input->SetAppid($appid);
            $input->SetMch_id($mchid);
            $input->SetOp_user_id($mchid);
            $input->SetRefund_fee($fee);
            $input->SetTotal_fee($fee);
            $input->SetOut_refund_no($refund_order['paycode']);
            $input->SetOut_trade_no($out_trade_no);
            $result = $WxPayApi->refund($input, 6, $path_cert, $path_key, $key);
            
            if ($result['result_code'] == 'SUCCESS') {//退款成功
              /*function getaccess_token($_W){
                $res=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
                $appid=$res['appid'];
                $secret=$res['appsecret'];
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
                $data = curl_exec($ch);
                curl_close($ch);
                $data = json_decode($data,true);
                return $data['access_token'];
              }*/
              function set_msg($_W,$_GPC){
                  //$access_token = getaccess_token($_W);
                  $ress=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
                  $user=pdo_get('zh_gjw_shotpublish',array('id'=>$_GPC['id']));
                  $openid=pdo_get('zh_gjw_register',array('phone'=>$user['phone']));
                  $formwork ='{
                      "touser": "'.$openid["openid"].'",
                      "template_id": "'.$ress["templeid"].'",
                      "form_id":"'.$user['formid'].'",
                      "data": {
                          "keyword1": {
                              "value": "'.$user['title'].'",
                              "color": "#173177"
                          },
                          "keyword2": {
                              "value":"微拍审核未通过",
                              "color": "#173177"
                          }
                      }  
                  }';
                  $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$ress['accessToken']."";
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL,$url);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
                  curl_setopt($ch, CURLOPT_POST,1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
                  $data = curl_exec($ch);
                  curl_close($ch);
                 return $data;
              }
              echo set_msg($_W,$_GPC);
                if ($refund_order['upfee']==1) {
                  $res=pdo_update('zh_gjw_shotpublish',array('pay'=>2,'state'=>3,'upfee'=>2),array('id'=>$_GPC['id']));
                }else{
                  $res=pdo_update('zh_gjw_shotpublish',array('pay'=>2,'state'=>3),array('id'=>$_GPC['id']));
                }
                if($res){
                  message('拒审并退款成功',$this->createWebUrl('shotmanage',array()),'success');
                }else{
                      message('失败！','','error');
                }
            }else{
                message($result['err_code_des'],'','error');
            }
          }
        }else{
              message('失败！','','error');
        }
        
        
        // function getaccess_token($_W){
        //   $res=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
        //   $appid=$res['appid'];
        //   $secret=$res['appsecret'];
        //   $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
        //   $ch = curl_init();
        //   curl_setopt($ch, CURLOPT_URL,$url);
        //   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        //   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        //   $data = curl_exec($ch);
        //   curl_close($ch);
        //   $data = json_decode($data,true);
        //   return $data['access_token'];
        // }
        // function set_msg($_W,$_GPC){
        //     $access_token = getaccess_token($_W);
        //     $ress=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
        //     $user=pdo_get('zh_gjw_shotpublish',array('id'=>$_GPC['id']));
        //     $openid=pdo_get('zh_gjw_register',array('phone'=>$user['phone']));
        //     $formwork ='{
        //         "touser": "'.$openid["openid"].'",
        //         "template_id": "'.$ress["templeid"].'",
        //         "form_id":"'.$user['formid'].'",
        //         "data": {
        //             "keyword1": {
        //                 "value": "'.$user['title'].'（微拍审核未通过）",
        //                 "color": "#173177"
        //             },
        //             "keyword2": {
        //                 "value":"审核结果",
        //                 "color": "#173177"
        //             }
        //         }  
        //     }';
        //     $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
        //     $ch = curl_init();
        //     curl_setopt($ch, CURLOPT_URL,$url);
        //     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        //     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
        //     curl_setopt($ch, CURLOPT_POST,1);
        //     curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
        //     $data = curl_exec($ch);
        //     curl_close($ch);
        //    return $data;
        // }
        // echo set_msg($_W,$_GPC);
        // $res=pdo_update('zh_gjw_shotpublish',array('state'=>3),array('id'=>$_GPC['id']));
        // if($res){
        //   message('', 'referer', 'success');
        // }else{
        //       message('失败！','','error');
        // }
} 
    
if($_GPC['op']=='bzj'){
  include_once IA_ROOT . '/addons/zh_gjw/cert/WxPay.Api.php';
  load()->model('account');
  load()->func('communication');
  $WxPayApi = new WxPayApi();
  $input = new WxPayRefund();
  $path_cert = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_cert_' . $_W['uniacid'] . '.pem';
  $path_key = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_key_' . $_W['uniacid'] . '.pem';
  $refund_order =pdo_get('zh_gjw_shotpublish',array('id'=>$_GPC['id']));
        if (empty($refund_order['paycode'])) {
          message('发布时，支付未设置，所以此条未支付',$this->createWebUrl('shotmanage',array()),'success');
        }
        $res_fee=$refund_order['bzj'];
        $shotmoney =pdo_get('zh_gjw_shotmoney',array('uniacid'=>$_W['uniacid'])); 
        if ($refund_order['pay']==1) {
          $res_fee =$res_fee+$shotmoney['publishfee'];
          if ($refund_order['upfee']==1) {
          $res_fee =$res_fee+$shotmoney['upfee'];
          }
          if ($refund_order['topfee']==1) {
            $res_fee =$res_fee+$shotmoney['topfee'];
          }
          if ($refund_order['paycode']) { 
            $res=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
            $appid=$res['appid'];
            $key=$res['apikey'];
            $mchid=$res['mac_id'];
            $out_trade_no=$refund_order['paycode'];//商户订单号
            $refundfee = $refund_order['bzj']*100;
            $fee = $res_fee*100;
             //var_dump($fee);die;
            $input->SetAppid($appid);
            $input->SetMch_id($mchid);
            $input->SetOp_user_id($mchid);
            $input->SetRefund_fee($refundfee);
            $input->SetTotal_fee($fee);
            $input->SetOut_refund_no($refund_order['paycode']);
            $input->SetOut_trade_no($out_trade_no);
            $result = $WxPayApi->refund($input, 6, $path_cert, $path_key, $key);
            
            if ($result['result_code'] == 'SUCCESS') {//退款成功
              function getaccess_token($_W){
                $res=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
                $appid=$res['appid'];
                $secret=$res['appsecret'];
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
                $data = curl_exec($ch);
                curl_close($ch);
                $data = json_decode($data,true);
                return $data['access_token'];
              }
              function set_msg($_W,$_GPC){
                  $access_token = getaccess_token($_W);
                  $ress=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
                  $user=pdo_get('zh_gjw_shotpublish',array('id'=>$_GPC['id']));
                  $openid=pdo_get('zh_gjw_register',array('phone'=>$user['phone']));
                  $formwork ='{
                      "touser": "'.$openid["openid"].'",
                      "template_id": "'.$ress["templeid"].'",
                      "form_id":"'.$user['formid'].'",
                      "data": {
                          "keyword1": {
                              "value": "'.$user['title'].'",
                              "color": "#173177"
                          },
                          "keyword2": {
                              "value":"微拍审核未通过",
                              "color": "#173177"
                          }
                      }  
                  }';
                  $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL,$url);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
                  curl_setopt($ch, CURLOPT_POST,1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
                  $data = curl_exec($ch);
                  curl_close($ch);
                 return $data;
              }
              echo set_msg($_W,$_GPC);
                $res=pdo_update('zh_gjw_shotpublish',array('state'=>3),array('id'=>$_GPC['id']));
                if($res){
                  message('拒审并退款成功',$this->createWebUrl('shotmanage',array()),'success');
                }else{
                      message('失败！','','error');
                }
            }else{
                message($result['err_code_des'],'','error');
            }
          }
        }
        
} 
include $this->template('web/shotmanage');